#! /usr/bin/env python

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

_plotter = None

def init(name, size):
	global _plotter
	if name == 'pygame':
		_plotter = _PygamePlotter(size)
	elif name == 'tkinter':
		_plotter = _TkPlotter(size)

def show():
	_plotter.show()

def create():
	return _plotter.create()

class _BasePlotter:
	def __init__(self, size):
		self.size = size
		self.width, self.height = size
		self.lss = []

	def show(self):
		raise NotImplemented()

	def create(self):
		ls = []
		self.lss.append(ls)
		return ls

class _TkPlotter(_BasePlotter):
	def __init__(self, size):
		_BasePlotter.__init__(self, size)
		self.size = size

	def show(self):
		import Tkinter as tk
		root = tk.Tk()
		root.overrideredirect(1)
		root.geometry('+100+100')

		def key(evt):
			if evt.keycode == 27:
				root.quit()

		root.bind('<Key>', key)

		for ls in self.lss:
			w = tk.Canvas(root, bg='white', width=self.size[0], height=self.size[1])
			for p,c in ls:
				coord = (p[0]-5,p[1]-5,p[0]+5,p[1]+5)
				w.create_oval(*coord, fill=self._get_color(c))
			w.create_rectangle(0,0,self.size[0],self.size[1],outline='#ddd')
			w.create_line(0,self.size[1]/2,self.size[0],self.size[1]/2, fill='#eee')
			w.create_line(self.size[0]/2,0,self.size[0]/2,self.size[1], fill='#eee')
			w.pack(side='left')
			w.update()
		w.postscript(file='postscript.ps')
		root.mainloop()

	def _get_color(self, c):
		color = hex(c[0] << 16 | c[1] << 8 | c[2])
		color = color.replace('0x', '')
		color = color.rjust(6, '0')
		return '#' + color

class _PygamePlotter(_BasePlotter):
	def __init__(self, size):
		_BasePlotter.__init__(self, size)

	def _plot(self, screen, draw, xy, color):
		draw.circle(screen, color, xy, 5)
		draw.circle(screen, (0,0,0), xy, 5, 1)

	def show(self):
		import pygame
		pygame.init()

		white = (0xff, 0xff, 0xff)
		grey = (0xd0, 0xd0, 0xd0)
		grey_lighter = (0xe0, 0xe0, 0xe0)

		screen = pygame.display.set_mode(self.size)
		screen.fill(white)
		draw = pygame.draw


		draw.rect(screen, grey, (0,0,self.size[0],self.size[1]), 1)
		draw.line(screen, grey_lighter, (0,self.size[1]/2),(self.size[0],self.size[1]/2))
		draw.line(screen, grey_lighter, (self.size[0]/2,0),(self.size[0]/2,self.size[1]))

		for xy,color in self.ls:
			self._plot(screen, draw, xy, color)

		clock = pygame.time.Clock()
		while True:
			clock.tick(1)
			for event in pygame.event.get():
					if event.type == pygame.QUIT:
						pygame.quit()

			pygame.display.flip()

